package com.dhecp.framework.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dhecp.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dhecp.project.base.domain.CompanyInfo;
import com.dhecp.project.base.service.CompanyInfoService;
import com.dhecp.project.flowable.mapper.SysProcessInstanceMapper;
import com.dhecp.project.system.domain.SysOrganizationInfo;
import com.dhecp.project.system.domain.SysUserBindRole;
import com.dhecp.project.system.domain.SysUserInfo;
import com.dhecp.project.system.domain.SysUserInfoSimple;
import com.dhecp.project.system.mapper.SysOrganizationInfoMapper;
import com.dhecp.project.system.mapper.SysUserInfoMapper;
import com.dhecp.project.system.mapper.SysUserInfoSimpleMapper;

/**
 * 自定义函数工具 获取指定值
 * @author DH-Xxj
 * @version 2019年12月13日
 */
@Service
public class CustomFunctionUtils {

    @Autowired
    private SysUserInfoSimpleMapper userInfoSimpleMapper;
    
    @Autowired
    private SysUserInfoMapper userInfoMapper;
    
    @Autowired
    private SysOrganizationInfoMapper organizationInfoMapper;
    
    @Autowired
    private CompanyInfoService companyInfoService;
    
    @Autowired
    private SysProcessInstanceMapper processInstanceMapper;
    
    /**
     * 取流程下一环节审批人
     * @param userList
     * @param roleList
     * @param customType
     * @return List - SysUserInfoSimple
     * @author DH-Xxj
     */
    public List<SysUserInfoSimple> queryActivitiNextUser(List<String> userList, List<String> roleList, String customType, Map<String, Object> moduleInfoMap){
    	List<SysUserInfoSimple> userInfoList = new ArrayList<SysUserInfoSimple>();
    	List<SysUserBindRole> userBindRoleList = new ArrayList<SysUserBindRole>();
    	String userIds = "";
    	String roles = "";
    	String managers = "";

    	if (moduleInfoMap != null){		//通过存储过程 Prob_ActProcessDefinedUserList 获取自定义审核用户列表
    		if (moduleInfoMap.get("moduleTable") == null || moduleInfoMap.get("moduleTable") == ""){
    			moduleInfoMap.put("moduleTable", "moduleTable");
    		}
    		if (moduleInfoMap.get("moduleNid") == null || moduleInfoMap.get("moduleNid") == ""){
    			moduleInfoMap.put("moduleNid", "moduleNid");
    		}
    		if (moduleInfoMap.get("userId") == null || moduleInfoMap.get("userId") == ""){
    			moduleInfoMap.put("userId", "userId");
    		}
    		if (moduleInfoMap.get("cType") == null || moduleInfoMap.get("cType") == ""){
    			moduleInfoMap.put("cType", "cType");
    		}
    		List<String> userListDefined = processInstanceMapper.findActProcessDefinedUserList(moduleInfoMap);
        	if (userListDefined != null && userListDefined.size() > 0){
        		for (String user : userListDefined){
        			userList.add(user);
        		}
         	}
    	}

    	if ("cmbId".equals(customType)){	//取公司经营管理者
    		Map<String, Object> params = new HashMap<String, Object> ();
    		String vMuser = "";
    		params.put("vId", ShiroUtils.getMandatorCid());
    		CompanyInfo companyInfo = new CompanyInfo();
			try {
				companyInfo = companyInfoService.queryCompanyInfoByVId(params);
			} catch (Exception e) {
				companyInfo = null;
			}
    		if (vMuser != null && !"".equals(vMuser)){
        		userList.add(vMuser);
    		}
    	}
    	if ("cmaId".equals(customType)){	//取公司机构所有管理者
    		String orgid = ShiroUtils.getMandatorCid();
    		SysOrganizationInfo orgInfo = organizationInfoMapper.findOrganizationInfoByOrgid(orgid);
    		if (orgInfo != null){
        		managers = orgInfo.getOrgManagersAll().toString();
    		}
    	}
    	if ("cmId".equals(customType)){		//取公司机构直接管理者
    		String orgid = ShiroUtils.getMandatorCid();
    		SysOrganizationInfo orgInfo = organizationInfoMapper.findOrganizationInfoByOrgid(orgid);
    		if (orgInfo != null){
        		managers = orgInfo.getOrgManagers().toString();
    		}
    	}
    	if ("tdmaId".equals(customType)){	//取顶级部门所有管理者
    		String orgPathAll = ShiroUtils.getMandatorDeptPath();
    		SysOrganizationInfo orgInfo = organizationInfoMapper.findTopDeptInfoByOrgPathAll(orgPathAll);
    		if (orgInfo != null){
        		managers = orgInfo.getOrgManagersAll().toString();
    		}
    	}
    	if ("tdmId".equals(customType)){	//取顶级部门直接管理者
    		String orgPathAll = ShiroUtils.getMandatorDeptPath();
    		SysOrganizationInfo orgInfo = organizationInfoMapper.findTopDeptInfoByOrgPathAll(orgPathAll);
    		if (orgInfo != null){
        		managers = orgInfo.getOrgManagers().toString();
    		}
    	}
    	if ("pdmaId".equals(customType)){	//取上级部门所有管理者
    		String orgPathAll = ShiroUtils.getMandatorDeptPath();
    		SysOrganizationInfo orgInfo = organizationInfoMapper.findParentOrganizationInfoByOrgPathAll(orgPathAll);
    		if (orgInfo != null){
        		managers = orgInfo.getOrgManagersAll().toString();
    		}
    	}
    	if ("pdmId".equals(customType)){	//取上级部门直接管理者
    		String orgPathAll = ShiroUtils.getMandatorDeptPath();
    		SysOrganizationInfo orgInfo = organizationInfoMapper.findParentOrganizationInfoByOrgPathAll(orgPathAll);
    		if (orgInfo != null){
        		managers = orgInfo.getOrgManagers().toString();
    		}
    	}
    	if ("dmaId".equals(customType)){	//取部门所有管理者
    		String orgPathAll = ShiroUtils.getMandatorDeptPath();
    		SysOrganizationInfo orgInfo = organizationInfoMapper.findOrganizationInfoByOrgPathAll(orgPathAll);
    		if (orgInfo != null){
        		managers = orgInfo.getOrgManagersAll().toString();
    		}
    	}
    	if ("dmId".equals(customType)){		//取部门直接管理者
    		String orgPathAll = ShiroUtils.getMandatorDeptPath();
    		SysOrganizationInfo orgInfo = organizationInfoMapper.findOrganizationInfoByOrgPathAll(orgPathAll);
    		if (orgInfo != null){
        		managers = orgInfo.getOrgManagers().toString();
    		}
    	}
    	if ("duId".equals(customType)){		//取部门所有员工
    		String orgPathAll = ShiroUtils.getMandatorDeptPath();
    		Map<String, Object> pathParams = new HashMap<String, Object>();
    		pathParams.put("orgPathAll", orgPathAll);
    		List<SysUserInfo> list= userInfoMapper.findUserListByOrgPathAll(pathParams);
    		if (list != null && list.size() > 0){
    			for (SysUserInfo user : list){
        			userList.add(user.getUserId().toString());
    			}
    		}
    	}
    	if ("luId".equals(customType)){		//取用户直属上级
    		String userLeader = ShiroUtils.getMandatorLeader();
    		if (userLeader != null && !"".equals(userLeader)){
        		userList.add(userLeader);
    		}
    	}
    	
    	//若获取到管理者，格式转换后追加到userIds中备查
		if (!"".equals(managers)){
			managers = managers.replaceAll("\\[", "'").replaceAll("\\]", "'");
			if ("".equals(userIds)){
				userIds = managers;
			}else{
				userIds = userIds + "," + managers;
			}
		}
    	
    	//查询拥有指定角色的用户编号并放到userList中
    	if (roleList != null && roleList.size() > 0){
    		for (String role : roleList){
    			if ("".equals(roles)){
    				roles = "'" + role + "'";
        		}else {
        			if (roles.toLowerCase().indexOf(role.toLowerCase()) == -1){
        				roles = roles + ",'" + role + "'";
        			}
        		}
    		}
    		if (!"".equals(roles)){
    			userBindRoleList = userInfoSimpleMapper.findUserBindRoleByRoleGuid(roles);
    		}
    		if (userBindRoleList != null && userBindRoleList.size() > 0){
    			for (SysUserBindRole userBindRole : userBindRoleList){
    				userList.add(userBindRole.getUserId().toString());
    			}
    		}
    	}
    	
    	//查询userList中所有用户信息
    	if (userList != null && userList.size() > 0){
    		for (String user : userList){
				if(!StringUtils.isEmpty(user)) {
					if ("".equals(userIds)) {
						userIds = "'" + user + "'";
					} else {
						if (userIds.toLowerCase().indexOf(user.toLowerCase()) == -1) {
							userIds = userIds + ",'" + user + "'";
						}
					}
				}
    		}
     	}
		if (!"".equals(userIds)){
			Map<String, Object> params = new HashMap<String, Object> ();
	        params.put("mandator", ShiroUtils.getMandatorId());
	        params.put("userIds", userIds);
	        params.put("leaveOffice", "leaveOffice");	//仅未离职员工
			userInfoList = userInfoSimpleMapper.findUserSimpleListByUserIds(params);
		}
    	return userInfoList;
    }
}
